import { error } from "cc";
import { Message, Request, Response } from "./IMessage";

const typeOpcodes = new Map<any, number>();
const opcodeTypes = new Map<number, any>();
export function message(opcode: number) {
    return function (target: any) {
        opcodeTypes.set(opcode, target.prototype);
        typeOpcodes.set(target.prototype, opcode);
    }
}
const requestResponse = new Map<any, any>();
export function responseType(responseType: new () => Message) {
    return function (target: any) {
        requestResponse.set(target.prototype, responseType.prototype);
    }
}
export class OpcodeTypeManager extends et.Entity {
    public static Instance: OpcodeTypeManager;


    public GetOpcode(type: new () => Message): number {
        return typeOpcodes.get(type.prototype);
    }

    public GetType(opcode: number) {
        return opcodeTypes.get(opcode)?.constructor as new () => any;
    }

    public GetResponseType(request: new () => Request) {
        let response = requestResponse.get(request.prototype);
        if (!response) {
            throw new error(`not found response type, request type: ${request.name}`);
        }
        return response.constructor as new () => any;
    }
}